package top.txl.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.txl.entity.AddressBook;
import top.txl.entity.R;
import top.txl.exception.CustomerException;
import top.txl.service.AddressBookService;
import top.txl.service.LoginService;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/address")
public class AddressBookController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private AddressBookService addressBookService;

    @PostMapping("/add")
    public R<String> addAddress(HttpServletRequest request, @RequestBody AddressBook book){
        log.info("addAddress,{}",book);
        String jwt = request.getHeader("Authorization");
        Integer userId = loginService.findUserByToken(jwt);

        book.setCreateTime(LocalDateTime.now());
        book.setCreateUser(userId);
        book.setUpdateTime(LocalDateTime.now());
        book.setUpdateUser(userId);
        book.setUserId(userId);

        addressBookService.addBook(book);

        return  R.success("操作成功");
    }


    @PutMapping("/edit")
    public R<String> updateAddress(HttpServletRequest request, @RequestBody AddressBook book){
        log.info("editAddress,{}",book);
        String jwt = request.getHeader("Authorization");
        Integer userId = loginService.findUserByToken(jwt);

        book.setUpdateTime(LocalDateTime.now());
        book.setUpdateUser(userId);
        book.setUserId(userId);

        if(book.getIsDefault() == null){
            book.setIsDefault(0);
        }

        if(book.getIsDefault() == 1){
            // 因为默认地址只能选择一个，需要修改其他项为0
            addressBookService.updateBookNotEqualThisId(book);
        }

        addressBookService.updateBook(book);

        return  R.success("操作成功");
    }

    @GetMapping("/lists")
    public R<List<AddressBook>> getAddressLists(HttpServletRequest request){
        log.info("lists");
        String jwt = request.getHeader("Authorization");
        Integer userId = loginService.findUserByToken(jwt);

        if(userId == null){
            throw new CustomerException("token已过期，请前往登录");
        }

        List<AddressBook> lists = addressBookService.getBookLists(userId);

        return  R.success(lists);
    }

    @GetMapping("/getIsDefaultAddress")
    public R<List<AddressBook>> getIsDefaultAddress(HttpServletRequest request){
        log.info("getIsDefaultAddress");
        String jwt = request.getHeader("Authorization");
        Integer userId = loginService.findUserByToken(jwt);

        List<AddressBook> lists = addressBookService.getIsDefaultAddress(userId);

        return R.success(lists);
    }

    @GetMapping("/detail/{id}")
    public R<AddressBook> getBookDetail(HttpServletRequest request,@PathVariable Integer id){
        log.info("bookDetail");
        String jwt = request.getHeader("Authorization");
        Integer userId = loginService.findUserByToken(jwt);

        if(id == null){
            throw new CustomerException("id参数为空");
        }

        AddressBook book = addressBookService.getBookDetail(userId,id);

        return  R.success(book);
    }
}
